package ro.imc.ums.domnica;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;

import ro.imc.database.domnica.StudentCrud;

public class GUI {
	private JFrame window;
	
	private JPanel panel;
	private JLabel greetingsMessage;
	private JComboBox switcher;
	private JPanel studentPanel = new JPanel();
	private JPanel profesorPanel = new JPanel();
	
	/*
	 * Misc
	 */
	private String selectedIndex="-1";
	private boolean studentStatusIndex=true;
	private List<Person> studentList;
	private List<Person> profesorList;
	private List<Person> studentListPrint;
	private List<Person> profesorListPrint;
	/*
	 * Buttons
	 */
	private JButton exit;
	private JButton addStudent;
	private JButton addProfesor;
	private JButton listStudent;
	private JButton listProfesor;
	
	/*
	 * User Variables
	 */
	private JTextField firstName = new JTextField("First Name");
	private JTextField lastName = new JTextField("Last Name");
	private JTextField id = new JTextField("ID");
	private JTextField studentNo = new JTextField("Student Number");
	private JComboBox status = new JComboBox();
	private JTextField title = new JTextField("Title");
	private JTextField field = new JTextField("Field");
	private JTextField cnp = new JTextField("CNP");
	private JTextArea listView;
	private JScrollPane listViewScroll;
	
	public GUI(){
		createInterface();
		createPanel();
		createGreetingsMessage();
		createListView();
		createWindowSwitcher();
		createExitButton();	
		createPanelStudent();
		createPanelProfesor();
		window.setVisible(true);	
	}
	
	/*
	 * JFrame Interface
	 */
	private void createInterface(){
		window = new JFrame();
		window.setTitle("UMS");
		window.setSize(900,700);
		window.setResizable(false);
		window.setLayout(new BorderLayout());
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	/*
	 * Main Panel
	 */
	private void createPanel(){
		panel = new JPanel();		
		panel.setLayout(null);	
		panel.setBackground(Color.WHITE);
		window.add(panel);
	}
	
	/*
	 * Greetings Message
	 */
	private void createGreetingsMessage(){
		greetingsMessage = new JLabel("Welcome to our kickass UMS!");
		greetingsMessage.setSize(350,25);
		greetingsMessage.setLocation(150,50);
		greetingsMessage.setForeground(Color.red);
		greetingsMessage.setFont(new Font("Serif", Font.ITALIC, 25));
		panel.add(greetingsMessage);
	}
	
	
	/*
	 * List View
	 */
	private void createListView(){
		listView = new JTextArea(16,16);
		listViewScroll = new JScrollPane(listView, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		listView.setSize(230,510);
		listView.setLocation(650,90);	
		listView.setEditable(false);
		listView.setBackground(Color.LIGHT_GRAY);

		listView.setVisible(false);
	
		panel.add(listView);
		panel.add(listViewScroll);
	
	}
	
	/*
	 * Student Panel
	 * 
	 */
	private void createPanelStudent(){	
		panel.add(studentPanel);
	}
	private void createStudentPanel(){
		
		studentPanel.setLayout(null);
		studentPanel.setSize(500,500);
		studentPanel.setLocation(50,50);
		studentPanel.setBackground(Color.LIGHT_GRAY);
		studentPanel.setVisible(false);
		createButtonsStudent();
		addNewStudent();
		
	}
	
	
	/*
	 * Profesor Panel
	 */
	private void createPanelProfesor(){	
		panel.add(profesorPanel);
	}
	private void createProfesorPanel(){
	
		profesorPanel.setLayout(null);
		profesorPanel.setSize(500,500);
		profesorPanel.setLocation(50,50);
		profesorPanel.setBackground(Color.LIGHT_GRAY);
		profesorPanel.setVisible(false);
		addNewProfesor();
		createButtonsProfesor();
	
	}
	
	/*
	 * Close Application Button
	 */
	private void createExitButton(){
		exit = new JButton("Exit", null);
		exit.setSize(85,25);
		exit.setLocation(760,625);
		exit.setBackground(Color.red);
		
		ActionListener actionExitButton = new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
				
			}
			
		};
		exit.addActionListener(actionExitButton);
		panel.add(exit);
		
	}
	
	
	/*
	 * Switcher Between Panels
	 */
	private void createWindowSwitcher(){
		String[] index = {"Student","Profesor","Groups","Courses","Exams","Catalog","Portfolio"};
		switcher = new JComboBox(index);
		switcher.setSelectedIndex(-1);
		switcher.setSize(150,25);
		switcher.setLocation(650,50);
		switcher.setEnabled(true);
		switcher.setBackground(Color.WHITE);
	
		
		ActionListener actionIndex = new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				JComboBox cb = (JComboBox) arg0.getSource();
				selectedIndex = (String) cb.getSelectedItem();
				System.out.println(selectedIndex);
				switch(selectedIndex){
				case "Student":
					createStudentPanel();			
					studentPanel.setVisible(true);
					profesorPanel.setVisible(false);
					greetingsMessage.setVisible(false);	
					listView.setText("");
					break;
				
				case "Profesor":
					createProfesorPanel();				
					profesorPanel.setVisible(true);
					studentPanel.setVisible(false);
					greetingsMessage.setVisible(false);
					listView.setText("");
					break;
				}
				
			}
			
		};
		
		
		
		switcher.addActionListener(actionIndex);
		panel.add(switcher);
	}

	
	private void addNewStudent(){
	
		firstName.setSize(150,25);
		firstName.setLocation(75,75);
		firstName.setBackground(Color.WHITE);
		
		lastName.setSize(150,25);
		lastName.setLocation(250,75);
		lastName.setBackground(Color.WHITE);
		
		cnp.setSize(150,25);
		cnp.setLocation(75,125);
		cnp.setBackground(Color.WHITE);
		
		studentNo.setSize(150,25);
		studentNo.setLocation(75,225);
		studentNo.setBackground(Color.WHITE);		
		
		id.setSize(150,25);
		id.setLocation(75,175);
		id.setBackground(Color.WHITE);		
		
		String[] studentStatus = {"Active", "Inactive"};
		status = new JComboBox(studentStatus);
		status.setSelectedIndex(0);
		status.setSize(100,25);
		status.setLocation(75,275);
		status.setBackground(Color.WHITE);
		
		ActionListener changeStudentStatus = new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				JComboBox cb = (JComboBox) e.getSource();
				String x = (String) cb.getSelectedItem();
				if(x == "Active"){
					studentStatusIndex = true;
				} else if(x == "Inactive"){
					studentStatusIndex = false;
				}
				
			}
			
		};
		
		status.addActionListener(changeStudentStatus);
		studentPanel.add(firstName);
		studentPanel.add(lastName);
		studentPanel.add(id);
		studentPanel.add(studentNo);
		studentPanel.add(cnp);
		studentPanel.add(status);
		
	}
	
	private void addNewProfesor(){
	
		
		firstName.setSize(150,25);
		firstName.setLocation(75,75);
		firstName.setBackground(Color.WHITE);
		
		lastName.setSize(150,25);
		lastName.setLocation(250,75);
		lastName.setBackground(Color.WHITE);
		
		cnp.setSize(150,25);
		cnp.setLocation(75,125);
		cnp.setBackground(Color.WHITE);

		
		id.setSize(150,25);
		id.setLocation(75,175);
		id.setBackground(Color.WHITE);		
			
		title.setSize(150,25);
		title.setLocation(75,225);
		title.setBackground(Color.WHITE);
		
		field.setSize(150,25);
		field.setLocation(75,275);
		field.setBackground(Color.WHITE);
		
		profesorPanel.add(firstName);
		profesorPanel.add(lastName);
		profesorPanel.add(id);		
		profesorPanel.add(cnp);
		profesorPanel.add(title);
		profesorPanel.add(field);
	
		
	}
	
	/*
	 * Add and List Buttons
	 */
	private void createButtonsStudent(){
		addStudent = new JButton("Add", null);
		addStudent.setSize(85,25);
		addStudent.setLocation(280,400);
		addStudent.setBackground(Color.red);
		
		listStudent = new JButton("List", null);
		listStudent.setSize(85,25);
		listStudent.setLocation(380,400);
		listStudent.setBackground(Color.red);
		studentPanel.add(addStudent);	
		studentPanel.add(listStudent);
	
	ActionListener actionAddNewStudent = new ActionListener(){

		@Override
		public void actionPerformed(ActionEvent e) {
		
			
			String studentFirstName = firstName.getText();
			String studentLastName = lastName.getText();
			int studentId = Integer.parseInt(id.getText().trim());
			int studentNumber = Integer.parseInt(studentNo.getText());
			boolean status = studentStatusIndex;
			String studentCnp = cnp.getText();
			try{
				Person.is_valid(studentCnp);
			} catch (CustomFormatException cfe){
				cfe.printStackTrace();
				JOptionPane.showMessageDialog(null, "Scoateti buletinul si mai incearca!");
			}
			studentList = new ArrayList<Person>();
			studentList.add(new Student(studentFirstName, studentLastName, studentId, studentCnp, studentNumber, status));
		
			
			StudentCrud sc = new StudentCrud();
			sc.insert((Object)new Student(studentFirstName, studentLastName, studentId, studentCnp, studentNumber, status));
			
			ArrayList<Object> al = new ArrayList<Object>();
			al = (ArrayList<Object>)sc.read();
			ArrayList<Student> al2 = new ArrayList<Student>();
			for(Object o : al) {
				al2.add((Student)o);
			}
			for (int i = 0; i < al2.size(); i++){
				System.out.println("");
				System.out.format("%20s", al2.get(i).getLastName() + " | ");
				System.out.format("%20s", al2.get(i).getFirstName()+ " | ");
				System.out.format("%20s", al2.get(i).getCnp() + " | ");
				System.out.format("%20s", al2.get(i).getStudentNo()+ " | ");
				System.out.format("%20s", al2.get(i).isStatus() + " | ");
			}
			EncodeXML obj = new EncodeXML();
			obj.encodeXML(studentList,studentFirstName,"Student");			
			
			
			
		}
		
	};
	
	ActionListener actionListNewStudent = new ActionListener(){

		@Override
		public void actionPerformed(ActionEvent arg0) {
			String s = "";
			studentListPrint = new ArrayList<Person>();
			DecodeXML obj = new DecodeXML();
			studentListPrint = obj.decodeXML("D:\\XML\\Student");
			
			for(Person student : studentListPrint){
			   s = s +  
					student.getFirstName() + "\n" +
				    student.getLastName() + "\n" +				
				    student.getId() + "\n" +
				    ((Student) student).getStudentNo() + "\n" +
				    student.getCnp() + "\n" + 
				    ((Student) student).isStatus() + "\n" + "\n";	
			  
	      }
			
			listView.setText(s);	
			listView.setVisible(true);
			
			}	
	};
	
		addStudent.addActionListener(actionAddNewStudent);
		listStudent.addActionListener(actionListNewStudent);
	}
	
	private void createButtonsProfesor(){
		addProfesor = new JButton("Add", null);
		addProfesor.setSize(85,25);
		addProfesor.setLocation(280,400);
		addProfesor.setBackground(Color.red);
		
		listProfesor = new JButton("List", null);
		listProfesor.setSize(85,25);
		listProfesor.setLocation(380,400);
		listProfesor.setBackground(Color.red);
		profesorPanel.add(addProfesor);	
		profesorPanel.add(listProfesor);
	
	
	ActionListener actionAddNewProfesor = new ActionListener(){

		@Override
		public void actionPerformed(ActionEvent arg0) {
		  
			
			String profesorFirstName = firstName.getText();
			String profesorLastName = lastName.getText();
			int profesorId = Integer.parseInt(id.getText().trim());
			String profesorCnp = cnp.getText();
			try{
				Person.is_valid(profesorCnp);
			} catch (CustomFormatException cfe){
				cfe.printStackTrace();
				JOptionPane.showMessageDialog(null, "Scoateti buletinul si mai incearca!");				
			}
						
			String profesorTitle = title.getText();
			String profesorField = field.getText();
		
			profesorList = new ArrayList<Person>();
			profesorList.add(new Profesor(profesorFirstName, profesorLastName, profesorId, profesorCnp, profesorTitle, profesorField));
						
			EncodeXML obj = new EncodeXML();
			obj.encodeXML(profesorList, profesorFirstName, "Profesor");			
		 
		  }
		
		
	};
	
	ActionListener actionListNewProfesor = new ActionListener(){

		@Override
		public void actionPerformed(ActionEvent e) {
			String s = "";
			profesorListPrint = new ArrayList<Person>();
			DecodeXML obj = new DecodeXML();
			profesorListPrint = obj.decodeXML("D:\\XML\\Profesor");
			
			for(Person profesor : profesorListPrint){
			   s = s +  
					profesor.getFirstName() + "\n" +
				    profesor.getLastName() + "\n" +				
				    profesor.getId() + "\n" +
				    ((Profesor) profesor).getField() + "\n" +
				    profesor.getCnp() + "\n" + 
				    ((Profesor) profesor).getTitle() + "\n" + "\n";	
   	       }
			listView.setText(s);	
			listView.setVisible(true);
		}
		
	};
	
	addProfesor.addActionListener(actionAddNewProfesor);
	listProfesor.addActionListener(actionListNewProfesor);
	}

}
